package com.atguigu.juc.chap02;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Taeyang
 * @date 2025/3/31
 */
public class ReentrantLockDemo02 {

    private static final ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(() -> {
            lock.lock();
            try {
                System.out.println("A begin");
                Thread.sleep(1000);
                System.out.println("A end");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }, "A").start();

        new Thread(() -> {
            lock.lock();
            try {
                System.out.println("B begin");
                Thread.sleep(2000);
                System.out.println("B end");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }, "B").start();
    }

}
